package com.wuseguang.springcloud.thread;

import com.alibaba.fastjson.JSONObject;
import com.wuseguang.springcloud.JdbcTemplate.HaiweidaCooperativeSchoolJdbc;
import com.wuseguang.springcloud.JdbcTemplate.WXJdbc;
import com.wuseguang.springcloud.dao.jdbc.StudentsLeaveSchoolJdbc;
import com.wuseguang.springcloud.util.DateUtil;
import com.wuseguang.springcloud.util.SpringBeanUtil;
import com.wuseguang.springcloud.util.WeChat.TemplateTools;
import com.wuseguang.springcloud.util.WeChat.WXUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 放学推送线程
 */
@Component
public class AfterSchoolThread implements Runnable {



    private Logger logger = LoggerFactory.getLogger(this.getClass());


    private StudentsLeaveSchoolJdbc studentsLeaveSchoolJdbc;

    private WXJdbc wXJdbc;

    private Integer studentId;

    private String teacherName;

    private Integer state;

    private String date;

    @Override
    public void run() {

        try {
            wXJdbc = SpringBeanUtil.getBean(WXJdbc.class);
            studentsLeaveSchoolJdbc = SpringBeanUtil.getBean(StudentsLeaveSchoolJdbc.class);
            //获取token
            String accessToken = wXJdbc.getOfficialAccountAccessToken(0);

            //查询家长的小程序openId
            String sql = "select p.open_id_one,p.id,p.open_id_two  from parent p  LEFT JOIN parent_student ps on(ps.parent_id=p.id)where ps.student_id=?";

            //家长openid
            Map<String, Object> parent = wXJdbc.queryMap(sql, studentId);
            String openId = (String) parent.get("open_id_two");

            sql = "select student_name string from student where id = ?";
            String studentName = studentsLeaveSchoolJdbc.queryString(sql,studentId);

            //判断openId是否为空
            if (!StringUtils.isEmpty(openId)) {


                Map<String, Object> pushMap = new HashMap<>();
                pushMap.put("first", "您好，尊敬的" + studentName + "家长");
                pushMap.put("keyword1", state==1?"您的孩子所在的班级，已经于"+date+"放学，请您留意孩子准时到家。":"您的孩子所在班级已留校，留校到"+date+"，请您留意孩子准时到家。");
                pushMap.put("keyword2", teacherName);
                pushMap.put("keyword3", date);
                pushMap.put("remark", "请您随时关注孩子动态。");


                //跳转小程序信息
                JSONObject appletJson = new JSONObject();
                //appletJson.put("appid","wx30b606bac7775eba");
                //appletJson.put("pagepath","pages/evaluate/index");

                //解析后的数据
                JSONObject json = TemplateTools.initializationData(pushMap);
                //推送公众号信息
                WXUtil.officialAccountPush(accessToken, openId, "a9QdOrXqrfbnEPsl1-JxzVrSQhwlIU5bnTvoL8eBLhI", "", appletJson, json);
            }

        } catch (Exception e) {
            logger.error("e", e);
        }

    }

    public Integer getStudentId() {
        return studentId;
    }

    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}
